package com.example.car

import android.app.DatePickerDialog
import android.icu.util.Calendar
import android.os.Bundle
import androidx.activity.viewModels

import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.ToastUtils
import com.example.car.adapter.CarStopAdapter
import com.example.car.databinding.ActivityCarStopBinding
import com.example.car.vm.CarStopViewModel
import com.example.common.view.view.BaseMvvmActivity
import dagger.hilt.android.AndroidEntryPoint
import java.text.SimpleDateFormat
import java.util.Locale

@Route(path = "/car/CarStopActivity")
@AndroidEntryPoint
class CarStopActivity : BaseMvvmActivity<ActivityCarStopBinding>() {
    override fun getLayoutId(): Int =R.layout.activity_car_stop
    private val carStopViewModel:CarStopViewModel by viewModels()
    val carStopAdapter=CarStopAdapter()
    var mm:String=""
    var dd:String=""
    override fun initView() {
        super.initView()
        val calender =Calendar.getInstance()
        val year =calender.get(Calendar.YEAR)
        val month =calender.get(Calendar.MONTH)
        val day =calender.get(Calendar.DAY_OF_MONTH)
        mDataBinding.ivDateTime.setOnClickListener {
            val datePickerDialog = DatePickerDialog(this,{_,syear,smonth,sday->
                if (smonth+1 <10) {
                    mm="0${smonth+1}"
                }else{
                    mm="${smonth+1}"
                }
                if(sday<10){
                    dd="0$sday"
                }else{
                    dd="$sday"
                }
                mDataBinding.tvDateTime.text="${syear}-${mm}-${dd}"
                carStopViewModel.getCarStop()
            },year,month,day)
            datePickerDialog.show()
        }
        carStopViewModel.getCarStop()
        mDataBinding.rv.adapter=carStopAdapter
        mDataBinding.rv.layoutManager=LinearLayoutManager(this)
        carStopViewModel.mSuccessLiveData.observe(this){
            carStopAdapter.data.clear()
         //   carStopAdapter.data.addAll(it.data)
            carStopAdapter.data.addAll(it.data.filter {
                it.paymentTime.substring(0..9).equals(mDataBinding.tvDateTime.text.toString()) })
            carStopAdapter.notifyDataSetChanged()
        }
        carStopViewModel.mErrorLiveData.observe(this){
            ToastUtils.showLong(it)
        }

    }

}